package com.example.snowliwork4.adapter;

import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v4.app.INotificationSideChannel;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.snowliwork4.CartActivity;
import com.example.snowliwork4.MainActivity;
import com.example.snowliwork4.R;
import com.example.snowliwork4.Shop;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;

public class CartAdapter extends BaseAdapter {


    //获取上下文环境
    private Context context;
    //获取item布局
    private int layoutId;
    //数据源
    private List<Shop> shop;

    //商品数量
//    private int curnum = 0;
    private List<Integer> curnum = MainActivity.curnum;
    //商品列表
    private List<Shop> shoCart = MainActivity.shoCart;





    public CartAdapter(){

    }

    public CartAdapter(Context context, int layoutId, List<Shop> shop) {
        this.context = context;
        this.layoutId = layoutId;
        this.shop = shop;
    }

    @Override
    public int getCount() {
        return shop.size();
    }

    @Override
    public Object getItem(int position) {
        return shop.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取布局文件对象
        View view = LayoutInflater.from(context).inflate(layoutId,null);
        //获取布局文件的控件对象
        ImageView imgview = view.findViewById(R.id.cart_image);
        TextView tital = view.findViewById(R.id.cart_tital);
        TextView brief = view.findViewById(R.id.cart_brief);
        TextView price = view.findViewById(R.id.if_price);
        ImageButton add = view.findViewById(R.id.imgbtn_add);
        ImageButton sub = view.findViewById(R.id.imgbtn_sub);
        EditText count = view.findViewById(R.id.shop_count);



        //加载购物车页面时显示以前的数值
        count.setText(MainActivity.curnum.get(position)+"");

        //获取数据源中当前元素的位置
        Shop sho = shop.get(position);
        //把数据显示到对应的控件中去
        imgview.setImageResource(sho.getsImage());
        tital.setText(sho.getsDetail());
        brief.setText(sho.getsSafe());
        price.setText(sho.getsPrice());

        //实现商品数量的加减

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer integer = curnum.get(position) + 1;
                MainActivity.curnum.set(position,integer);
                count.setText(integer+"");

                //结算
                //获取商品总金额
                CartActivity.sumPrice=0.0;
                for(int i = 0;i<curnum.size();i++){
                    //获取i位置的商品数量
                    int counti = (int)MainActivity.curnum.get(i);
                    //获取i位置的商品价格
                    double pricei = Double.parseDouble(shoCart.get(i).getsPrice().substring(1));
                    //计算总金额
                    CartActivity.sumPrice = CartActivity.sumPrice + (counti * pricei);
                    CartActivity.sumPrice = (double)Math.round(CartActivity.sumPrice*100)/100;
                }
                CartActivity.sum.setText(CartActivity.sumPrice+"");

            }
        });


        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (MainActivity.curnum.get(position)>0){
                    Integer integer = curnum.get(position) - 1;
                    MainActivity.curnum.set(position,integer);
                    count.setText(integer+"");
                }
                //删除逻辑
                if (MainActivity.curnum.get(position) == 0){
                    Shop itmStr = MainActivity.shoCart.get(position);
                    MainActivity.shoCart.remove(itmStr);
                    Integer i = MainActivity.curnum.get(position);
                    MainActivity.curnum.remove(i);
                    notifyDataSetChanged();

                }

//                //结算
//                //获取商品总金额
                CartActivity.sumPrice=0.0;
                for(int i = 0;i<curnum.size();i++){
                    //获取i位置的商品数量
                    int counti = (int)MainActivity.curnum.get(i);
                    //获取i位置的商品价格
                    double pricei = Double.parseDouble(MainActivity.shoCart.get(i).getsPrice().substring(1));
                    //计算总金额
                    CartActivity.sumPrice = CartActivity.sumPrice + (counti * pricei);
                    CartActivity.sumPrice = (double) Math.round(CartActivity.sumPrice*100)/100;
                }
                CartActivity.sum.setText(CartActivity.sumPrice+"");


            }


        });










        return view;
    }
}
